package com.aaa.controller.rechargerule;

import com.aaa.entity.RecharGerule;
import com.aaa.entity.ResponseDto;
import com.aaa.service.RecharGeruleService;
import com.aaa.service.impl.RecharGeruleServiceImpl;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/UpdateRecharGeruleServlet")
public class UpdateRecharGeruleServlet extends HttpServlet {
    private RecharGeruleService recharGeruleService;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("name");
        String coefficient = req.getParameter("coefficient");
        String createdTime = req.getParameter("createdTime");
        String endTime = req.getParameter("endTime");
        String status = req.getParameter("status");
        String startMoney = req.getParameter("startMoney");
        String id = req.getParameter("id");

        RecharGerule recharGerule = new RecharGerule();

        recharGerule.setCoefficient(Double.parseDouble(coefficient));
        recharGerule.setCreatedTime(createdTime);
        recharGerule.setEndTime(endTime);
        recharGerule.setName(name);
        recharGerule.setStartMoney(Integer.parseInt(startMoney));
        recharGerule.setStatus(Integer.parseInt(status));
        recharGerule.setId(Integer.parseInt(id));
        System.out.println(recharGerule);
        int num = recharGeruleService.updateRecharGeruleById(recharGerule);
        ResponseDto responseDto = new ResponseDto();
        if (num > 0){
            responseDto.setStatus(ResponseDto.SUCCESS_CODE);
            responseDto.setMessage("更新成功");
        }else {
            responseDto.setStatus(ResponseDto.FAILURE_CODE);
            responseDto.setMessage("更新失败");
        }
        resp.getWriter().write(new Gson().toJson(responseDto));
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }

    @Override
    public void init() throws ServletException {
        recharGeruleService = new RecharGeruleServiceImpl();
    }
}
